<?php use_stylesheet('orders.css') //load css pf order page   ?>
<?php use_stylesheet('forms.css') //load css pf order page   ?>
<?php use_stylesheet('hoverTables.css') //load css of tables  ?>
<!-- All forms that used newsucces or editsucces are in a div with the id form -->
<div id="form"> 
    <!-- the go back button on the side -->
    <a href="<?php echo url_for('sales/indexPnp') ?>" class="bt_back"><img src="<?php echo image_path('Buttons/bt_arrow_back.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_arrow_back_hov.png') ?>'" onmouseout="this.src='<?php echo image_path('Buttons/bt_arrow_back.png') ?>'"></a>


<?php //functions to add something to the order 
if(isset($_POST['add_item'])){
    $id = $_POST['id'];
    $unit = $_POST['unit'];
    $number = $_POST['number'];
    $cat = $_POST['cat'];
    $name = $_POST['name'];
    $type = $_POST['type'];
    $priceExcl = $_POST['priceExcl'];
    $priceIncl= $_POST['priceIncl'];
    addItem($unit, $id, $number, $cat, $name, $type, $priceExcl, $priceIncl);
    unset($_POST['add_item']);
}
function addItem($unit, $id, $number, $cat, $name, $type, $priceExcl, $priceIncl){
    //check if in list
    if(isset($_SESSION['pricelist'])){
        $inlist = false; //standard value
        foreach($_SESSION['pricelist'] as $value){  //go through list
            if($id == $value['id']){
                if($cat == $value['cat']){
                    $inlist = true;
                    echo '<span style="color:red;">The item has already been added to the list</span>';
                    break;
                }
            }
        }
        if($inlist== false){
            setVariables($unit, $id, $number, $cat, $name, $type, $priceExcl, $priceIncl);
        }
    } else {
        setVariables($unit, $id, $number, $cat, $name, $type, $priceExcl, $priceIncl);
    }
}
function setVariables($unit, $id, $number, $cat, $name, $type, $priceExcl, $priceIncl){
    $i = 0;
    while(isset($_SESSION['pricelist'][$i])){
        $i++;
    }
    $array = array('id'=>$id, 'cat'=>$cat, 'number'=>$number, 'name'=>$name, 'type'=>$type, 'priceExcl'=>$priceExcl, 'priceIncl'=>$priceIncl, 'unit'=>$unit);
    $_SESSION['pricelist'][$i] = $array;
}
if(isset($_POST['empty_list'])){
    if(isset($_SESSION['pricelist'])){
        unset($_SESSION['pricelist']);
    }
}

if(isset($_POST['page'])){
    $_SESSION['page']= true;
}
?>
<?php 
    if(!isset($_SESSION['page'])){
        //show all the possible products plus the shopping cart 
        ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
            <fieldset>  
                <legend class="bold">Materials &amp; Products for sale</legend>
                <table class="table_top">
                    <tr class="bold head_small">
                        <td width="70px">Item No.</td>
                        <td width="60px">Category</td>
                        <td width="170px">Name</td>
                        <td>Type</td>
                        <td width="120px">Price per Unit (excl. VAT)</td>
                        <td width="120px">Price per Unit (incl. VAT)</td>
                        <td width="35px"></td>
                    </tr>
                </table>
                <div id="scroll_div" class="table_div">
                    <table>
                        <!-- ugly solution to get the table right -->
                        <tr class="bold head_small" height="15px">
                            <td width="70px"></td>
                            <td width="60px"></td>
                            <td width="200"></td>
                            <td></td>
                            <td width="100px"></td>
                            <td width="40px"></td>
                            <td width="20px"></td>
                        </tr>
                    <?php foreach ($parts as $i=>$material): ?>
                        <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> ">
                            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                                <?php //calculate price with VAT
                                    $price = $material->getPrice();
                                    $VAT = $material->getVAT();
                                    $priceIncl = $price * (1 + ($VAT / 100));
                                ?>
                                <input type="hidden" name="number" value="<?php echo $material->getPartNumber() ?>"/>
                                <input type="hidden" name="cat" value="<?php echo 'Material' ?>"/>
                                <input type="hidden" name="name" value="<?php echo $material->getName() ?>"/>
                                <input type="hidden" name="priceExcl" value="<?php echo $material->getPrice() ?>"/>
                                <input type="hidden" name="priceIncl" value="<?php echo $priceIncl ?>"/>
                                <input type="hidden" name="type" value="<?php echo $material->getType() ?>"/>
                                <input type="hidden" name="id" value="<?php echo $material->getId() ?>"/>
                                <input type="hidden" name="unit" value="<?php echo $material->getUnit() ?>"/>
                                <td><?php echo $material->getPartNumber() ?></td>
                                <td><?php echo 'Material' ?></td>
                                <td><?php echo $material->getName() ?></td>
                                <td><?php echo $material->getType() ?></td>
                                <td><?php echo $material->getPrice() ?></td>
                                <td><?php echo $priceIncl ?></td>
                                <td><input type="submit" class="add_bt" value="" name="add_item" title="Add to order"/></td>
                            </form>
                        </tr>
                    <?php endforeach; ?>
                    <?php foreach ($products as $i=>$product): ?> 
                        <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> ">
                            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                                <?php //calculate price with VAT
                                    $price = $product->getPrice();
                                    $VAT = $product->getVAT();
                                    $priceIncl = $price * (1 + ($VAT / 100));
                                ?>
                                <input type="hidden" name="number" value="<?php echo $product->getProductNumber() ?>"/>
                                <input type="hidden" name="cat" value="<?php echo 'Product' ?>"/>
                                <input type="hidden" name="name" value="<?php echo $product->getName() ?>"/>
                                <input type="hidden" name="priceExcl" value="<?php echo $product->getPrice() ?>"/>
                                <input type="hidden" name="priceIncl" value="<?php echo $priceIncl ?>"/>
                                <input type="hidden" name="type" value="<?php echo $product->getType() ?>"/>
                                <input type="hidden" name="id" value="<?php echo $product->getId() ?>"/>
                                <input type="hidden" name="unit" value="<?php echo 'pcs' ?>"/>
                                <td><?php echo $product->getProductNumber() ?></td>
                                <td><?php echo 'Product' ?></td>
                                <td><?php echo $product->getName() ?></td>
                                <td><?php echo $product->getType() ?></td>
                                <td><?php echo $product->getPrice() ?></td>
                                <td><?php echo $priceIncl ?></td>
                                <td><input type="submit" class="add_bt" value="" name="add_item" title="Add to order"/></td>
                            </form>
                        </tr>
                    <?php endforeach; ?>    
                    </table>
                </div>
            </fieldset>
        </form>
        <?php if(isset($_SESSION['pricelist'])){ ?>
            <fieldset>
                <legend class="bold">Price list</legend>
                    <table>
                        <tr class="bold head_small">
                            <td width="50px">Item No.</td>
                            <td width="60px">Category</td>
                            <td>Name</td>
                            <td>Type</td>
                            <td width="80px">Price per Unit (excl. VAT)</td>
                            <td width="40px">Price per Unit (incl. VAT)</td>
                        </tr>
                        <?php foreach($_SESSION['pricelist'] as $value){ ?>
                        <tr>
                        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                            <td><?php echo $value['number'] ?></td>
                            <td><?php echo $value['cat'] ?></td>
                            <td><?php echo $value['name'] ?></td>
                            <td><?php echo $value['type'] ?></td>
                            <td><?php echo $value['priceExcl'] ?> </td>
                            <td><?php echo $value['priceIncl'] ?> </td>   
                        </form>
                        </tr>                      
                        <?php } ?>                      
                    </table>
                    <table>
                        <tr>
                            <td><input type="submit" name="empty_list" value="" class="bt_empty" title="Empty entire price list"/></td>
                            <td><input type="submit" value="" name="page" class="ok ok_margin" title="Continue to submit list"/></td>
                        </tr>
                    </table>
                </form>
            </fieldset>
        <?php } ?>
        <?php
    } else {
        ?>
        <form action="<?php echo url_for('sales/showPriceList') ?>" method="post" target="_blank">
            <fieldset>
                <legend class="bold">Add an optional note</legend>
                <table>
                    <tr>
                        <td class="left_title italic">Note:</td>
                        <td><input type="text" name="note" /></td>
                </table>
            </fieldset>              
                <input type="submit" value="" class="print" title="Print price list"/>
        </form>
        <?php
    }
?>
</div><!-- close div form -->